﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using AutoMapper;
using FikriKibar.Common.Helpers;
using FikriKibar.DataAccess.Abstract;
using FikriKibar.DataAccess.ContextBase;
using FikriKibar.DataAccess.RepositoryBase;
using FikriKibar.Entities.Concrete;

namespace FikriKibar.DataAccess
{
    public class EfLanguageDal : EfEntityRepositoryBase<Language, FikriKibarDbContext>, ILanguageDal
    {
        protected override Language AddEntity(Language entity, FikriKibarDbContext context)
        {
            return context.Languages.Add(entity);
        }

        protected override Language UpdateEntity(Language entity, FikriKibarDbContext context)
        {
            var oldEntity = GetEntity(entity, context);
            var newEntity = Mapper.Map(entity, oldEntity);
            context.Entry(oldEntity).CurrentValues.SetValues(newEntity);
            return newEntity;
        }

        protected override List<Language> GetEntities(FikriKibarDbContext context)
        {
            return context.Languages.ToList();
        }

        protected override List<Language> FilterEntities(FikriKibarDbContext context, Expression<Func<Language, bool>> where)
        {
            return context.Languages.Where(where).ToList();
        }
        protected override List<Language> FilterEntities(FikriKibarDbContext context, Expression<Func<Language, bool>> where, SortHelper<Language> sortHelper)
        {
            var query = (IOrderedQueryable<Language>)context.Languages.Where(where);
            query = sortHelper.GenerateOrderedQuery(query);
            return query.ToList();
        }

       
        protected override List<Language> FilterEntities(FikriKibarDbContext context, Expression<Func<Language, bool>> where, SortHelper<Language> sortHelper, int index, int size)
        {
            var query = (IOrderedQueryable<Language>)context.Languages.Where(where);
            query = sortHelper.GenerateOrderedQuery(query);
            return query.Skip((index - 1) * size).Take(size).ToList();
        }

        protected override Language GetEntity(Language entity, FikriKibarDbContext context)
        {
            return context.Languages.FirstOrDefault(e => e.Id == entity.Id);
        }

        public Language GetByCode(string code)
        {
            using (var context = new FikriKibarDbContext())
            {
                return context.Languages.FirstOrDefault(e => e.LanguageCode == code);
            }
        }

        public Language GetById(int id)
        {
            using (var context = new FikriKibarDbContext())
            {
                return context.Languages.FirstOrDefault(e => e.Id == id);
            }
        }

        public List<string> GetAllLanguageCodes()
        {
            using (var context = new FikriKibarDbContext())
            {
                return context.Languages.Select(e => e.LanguageCode).ToList();
            }
        }
    }
}
